package com.woniu.zsd.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.platform.dao.*;
import com.woniu.zsd.platform.model.Account;
import com.woniu.zsd.platform.model.AccountRole;
import com.woniu.zsd.platform.model.Role;
import com.woniu.zsd.platform.model.RoleResource;
import com.woniu.zsd.platform.service.AccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@Service("platformAccountServiceImpl")
@Primary
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements AccountRoleService {
    @Resource
    private AccountRoleMapper accountRoleMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleResourceMapper roleResourceMapper;
    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public void addRole(Integer accountId, Integer roleId) throws Exception {
        Account account = accountMapper.selectById(accountId);
        if (account == null){
            throw new Exception("账号不存在");
        }
        Role role = roleMapper.selectById(roleId);
        if (role == null){
            throw new Exception("角色不存在");
        }

        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", accountId);
        AccountRole accountRole = accountRoleMapper.selectOne(wrapper);
        if (accountRole == null){
            AccountRole ar = new AccountRole();
            ar.setAccountId(accountId);
            ar.setRoleId(roleId);
            accountRoleMapper.insert(ar);
            // 更新账号状态
            account.setStatus(1);
            accountMapper.updateById(account);

//            List<Integer> roleResourceIds = resourceMapper.findRoleResourceIds(roleId);

        }else{
            accountRole.setRoleId(roleId);
            accountRoleMapper.updateById(accountRole);
        }

    }
    private List<Integer> findeRoleResource(){
        return null;
    }

    @Override
    public void deleteRole(Integer accountId, Integer roleId) throws Exception {
        Account account = accountMapper.selectById(accountId);
        if (account == null){
            throw new Exception("账号不存在");
        }
        Role role = roleMapper.selectById(roleId);
        if (role == null){
            throw new Exception("角色不存在");
        }
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", accountId);
        AccountRole accountRole = accountRoleMapper.selectOne(wrapper);
        if (accountRole == null){
            throw new Exception("账号角色关系不存在");
        }
        account.setStatus(0);
        accountMapper.updateById(account);
        accountRoleMapper.deleteById(accountRole.getAccountRoleId());
    }
}
